package au.edu.utas.yu_qiao_group.luckyman.ui.raffleshome;

import androidx.recyclerview.widget.RecyclerView;

public class FloatingButtonScrollListener extends RecyclerView.OnScrollListener {
  private HideAndShowListener mHideAndShowListener;
  private static final int THRESHOLD = 20;
  private int distance = 0;
  private boolean visible = true;

  public FloatingButtonScrollListener(HideAndShowListener hideAndShowListener) {
    mHideAndShowListener = hideAndShowListener;
  }

  @Override
  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    if (distance > THRESHOLD && visible) {
      visible = false;
      mHideAndShowListener.hide();
      distance = 0;
    } else if (distance < -20 && !visible) {
      visible = true;
      mHideAndShowListener.show();
      distance = 0;
    }
    if (visible && dy > 0 || (!visible && dy < 0)) {
      distance += dy;
    }
  }

  public interface HideAndShowListener {
    void hide();

    void show();
  }
}
